package com.sjl.record

import android.Manifest
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import com.permissionx.guolindev.PermissionX
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private var isRecord = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        request.setOnClickListener {
            PermissionX.init(this as FragmentActivity?)
                .permissions(
                    listOf(
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                    )
                )
                .onExplainRequestReason { scope, deniedList ->
                    //拒绝解释
                    scope.showRequestReasonDialog(deniedList, "权限被禁止，请手动开启", "去开启", "取消")
                }
                .onForwardToSettings { scope, deniedList ->
                    //去开启
                    scope.showForwardToSettingsDialog(deniedList, "权限被禁止，请手动开启", "去开启", "取消")
                }
                .request { allGranted, grantedList, deniedList ->
                    if (allGranted) {
                        Toast.makeText(this, "权限请求成功", Toast.LENGTH_SHORT).show()
                    }
                }
        }
        mediaRecordAAC.setOnClickListener {
            if (!isRecord) {
                MediaRecorderUtil.getInstance().start()
            } else {
                MediaRecorderUtil.getInstance().stop()
            }
            isRecord = !isRecord
            Toast.makeText(this, "MediaRecord" + isRecord, Toast.LENGTH_SHORT).show()
        }
    }
}